//获取应用实例
var app = getApp()
const qiniuUploader = require("../../lib/qiniuUploader");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AlbumName: '',
    PrewUrl: '../../images/img_add.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindInput: function (event) {
    this.setData({
      AlbumName: event.detail.value,
    })
  },
  uploadImg: function (event) {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#155D3E",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths);
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // 交给七牛上传
        qiniuUploader.upload(tempFilePaths[0], (res) => {
          that.setData({
            PrewUrl: res.imageURL,
          });
        }, (error) => {
          console.log('error: ' + error);
        }, {
            region: app.globalData.region,
            uploadURL: app.globalData.uploadURL,
            domain: app.globalData.domain,
            uptokenURL: app.globalData.uptokenURL,
          }, (res) => {
            console.log('上传进度', res.progress)
            console.log('已经上传的数据长度', res.totalBytesSent)
            console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
          });
      }
    })
  },
  submit: function (event) {
    console.log(this.data.AlbumName)
    console.log(this.data.PrewUrl)
    if (this.data.AlbumName == '') {
      wx.showToast({
        title: '请输入电话簿名称！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    } else if (this.data.PrewUrl == '../../images/img_add.png') {
      wx.showToast({
        title: '请上传电话簿封面！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    } else {
      wx.request({
        url: app.globalData.ServiceUrl +'classify/saveClassify', //接口地址
        method: 'POST',
        data: {
          ValidateData: app.globalData.ValidateData,
          paras: {
            classifyName: this.data.AlbumName,
            classifyNote:'',
            classifyHeadurl: this.data.PrewUrl
          }
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
            wx.showToast({
              title: '新增电话簿成功！',
              icon: 'success',
              duration: 1000,
              complete: function () {
                //上传成功，关闭页面
                wx.navigateBack({})
               } //接口调用结束的回调函数  
            })
          } else {
            wx.showToast({
              title: '新增电话簿失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '新增电话簿失败！',
            image: '../../images/img_jinggao.png',
            duration: 1000
          })
        }
      })
    }
  }
})